package com.gxa.community.admin.service.content.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.gxa.community.admin.mapper.content.TopicGoodsMapper;
import com.gxa.community.admin.mapper.content.TopicMapper;
import com.gxa.community.admin.service.content.TopicService;
import com.gxa.community.core.pojo.domain.TopicInfo;
import com.gxa.community.core.pojo.dto.content.RelativeGood;
import com.gxa.community.core.pojo.dto.content.SingleTopicDTO;
import com.gxa.community.core.pojo.dto.content.TopicGoodsDto;
import com.gxa.community.core.pojo.vo.content.TopicGoodsVo;
import com.gxa.community.core.pojo.vo.content.TopicVo;
import com.gxa.community.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    private TopicMapper topicMapper;
    @Autowired
    TopicGoodsMapper topicGoodsMapper;

    @Override
    public R getSingleTopicById(Integer id) {
        SingleTopicDTO singleTopicDTO = new SingleTopicDTO();
        singleTopicDTO.setRelativeGoods(topicMapper.selectRelativeGoodsByTopicId(id));
        //获取专题中除了相关商品的信息
        TopicInfo topicInfo = topicMapper.selectById(id);
//        BeanUtils.copyProperties(topicInfo,singleTopicDTO);
        singleTopicDTO.setId(topicInfo.getId());
        singleTopicDTO.setDetails(topicInfo.getDetails());
        singleTopicDTO.setName(topicInfo.getName());
        singleTopicDTO.setPic(topicInfo.getPic());
        singleTopicDTO.setStatus(topicInfo.getStatus());
        singleTopicDTO.setTips(topicInfo.getTips());
        return singleTopicDTO!=null?R.ok("查询成功！").put("data",singleTopicDTO):R.error(600,"查询失败，请联系管理员！");
    }

    @Override
    public R addTopic(TopicVo topicVo) {
        //新建专题，先增加专题信息表，再增加专题商品表
        topicVo.setReleaseTime(System.currentTimeMillis());
        Integer result1 = topicMapper.insertTopicInfo(topicVo);
        //获取新增专题id
        Integer id = topicVo.getId();
        List<RelativeGood> relativeGoods = topicVo.getRelativeGoods();
        Integer result2 = 0;
        for (RelativeGood relativeGood : relativeGoods) {
            relativeGood.setTopicId(id);
            Integer integer = topicMapper.insertTopicGoods(relativeGood);
            if (integer > 0){
                result2++;
            }
        }
        return result1>0 && result2>0?R.ok("新增成功！"):R.error(600,"新增失败，请联系管理员");
    }

    //修改单个专题
    @Override
    public R updateTopicById(TopicVo topicVo) {
//        log.info("进入该方法了==========》");
        //先删除topic_goods表中相关信息
        topicMapper.deleteTopicGoodsByTopicId(topicVo.getId());
        //再修改topic_info表中相关信息
        Integer result1 = topicMapper.updateTopicInfo(topicVo);
        //再向topic_goods表中插入relativegoods数据
        List<RelativeGood> relativeGoods = topicVo.getRelativeGoods();
        Integer result2 = 0;
        for (RelativeGood relativeGood : relativeGoods) {
            relativeGood.setTopicId(topicVo.getId());
            Integer integer = topicMapper.insertTopicGoods(relativeGood);
            if (integer > 0){
                result2++;
            }
        }
        return result1>0 && result2>0?R.ok("修改成功！"):R.error(600,"修改失败，请联系管理员!");
    }


    //删除专题，需要删除topic_info与topic_goods表中相关信息
    @Override
    public R delTopicById(Integer id) {
        Integer result1 = topicMapper.deleteTopicInfoById(id);
        Integer result2 = topicMapper.deleteTopicGoodsByTopicId(id);
        return result1>0&&result2>0?R.ok("删除成功！"):R.error(600,"删除失败，请联系管理员！");
    }

    @Override
    public R updateTopicStatus(Integer id) {
        TopicInfo topicInfo = topicMapper.selectById(id);
        if (topicInfo.getStatus() == 1){
            Integer result = topicMapper.updateTopicInfoStatus(id, 0);
            return result>0?R.ok("修改成功！"):R.error(600,"修改失败，请联系管理员");
        }else {
            Integer result = topicMapper.updateTopicInfoStatus(id, 1);
            return result>0?R.ok("修改成功！"):R.error(600,"修改失败，请联系管理员");
        }
    }

    /**
     * 远程通信接口
     * 获取所有的专题包含商品信息
     * @return List<TopicGoodsDto>;
     */
    @Override
    public List<TopicGoodsDto> queryTopList() {
        List<TopicGoodsDto> topicGoodsDtos = new ArrayList<>();
        QueryWrapper <TopicInfo> wrappere = new QueryWrapper<>();
        wrappere.eq("status",1);
        List<TopicInfo> topicInfos = topicMapper.selectList(wrappere);
        for (TopicInfo topicInfo : topicInfos) {
            QueryWrapper<TopicGoodsVo> wrapper = new QueryWrapper<>();
            wrapper.eq("tid",topicInfo.getId());
            List<TopicGoodsVo> goods = topicGoodsMapper.selectList(wrapper);
            TopicGoodsDto dto = new TopicGoodsDto();
            dto.setGoods(goods);
            dto.setDetails(topicInfo.getDetails());
            dto.setName(topicInfo.getName());
            dto.setPic(topicInfo.getPic());
            dto.setTipe(topicInfo.getTips());
            dto.setId(topicInfo.getId());
            topicGoodsDtos.add(dto);
        }
        return topicGoodsDtos;
    }


}
